<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/16
 * Time: 11:07
 */

namespace app\index\controller;


use app\common\controller\HomeBase;
use think\Cache;
use \think\Image;
use think\Db;
use think\Controller;
use think\Session;
header('Access-Control-Allow-Origin:*');


class UserInfo extends HomeBase
{
    /*
     * 获取邀请人手机号
     * */
    public function getmobile(){
        if ($this->request->isGet()) {
            $id=$this->request->get();
            $mobile=Db::table('os_user')->where(['id'=>$id['id']])->field('mobile')->find();
            if($mobile){
                return json(['Status'=>1,'Message'=>'获取成功','data'=>$mobile['mobile']]);
            }else{
                return json(['Status'=>0,'Message'=>'获取失败']);
            }
        }else{
            return json(['Status'=>0,'Message'=>'参数错误']);
        }
    }


    /*
     * 获取邀请链接
     * */
    public function getlink(){
        if ($this->request->isGet()) {
            $link='http://www.zmair.cn/dist/index.html#/registered';// . $_SERVER['SERVER_NAME'] ;
            return json(['Status'=>1,'Message'=>'获取成功','data'=>$link]);
        }else{
            return json(['Status'=>0,'Message'=>'参数错误']);
        }
    }


    /*
     * 轮播图公共
     * */
    public function imgnotice(){
        $info = Db::name('system')->field('value')->where('name', 'img_notice')->find();
        $data = unserialize($info['value']);
        foreach ($data['image'] as $k=>$v){
            $data['image'][$k]='http://' . $_SERVER['SERVER_NAME'] .'/public/'. $v;
        }
        return json(['Status'=>1,'Message'=>'获取成功','data'=>$data]);
    }
    /*
     * 登录
     * mobile
     * password
     * */
    public function Login(){
        if ($this->request->isGet()) {
            $userlogin=$this->request->get();
            $where['mobile'] = $userlogin['mobile'];
            $return[] = null;
            $user = Db::table('os_user')->where($where)->find();
            if (!empty($user)) {
                $where['password'] = md5($userlogin['password']);
                $user = Db::table('os_user')->where($where)->find();
                if($user){
                    if($user['status']==1) {
                        $return = array('Status' => 1, 'Message' => '登录成功');
                    }else{
                        $return = array('Status' => 0, 'Message' => '禁止登陆');
                    }
                }else{
                    $return = array('Status' => 0, 'Message' => '密码错误');
                }
//                $return = empty($user) ? array('Status' => 0, 'Message' => '密码错误') : array('Status' => 1, 'Message' => '登录成功');
                if (!empty($user)) {
                    $last_login_time = date('Y-m-d H:i:s', time());
                    Db::table('os_user')->update(['last_login_time'=>$last_login_time,'id' => $user['id']]);
                    unset($user['password']);
                    $user['account_code_img'] = 'http://' . $_SERVER['SERVER_NAME'] .'/'. $user['account_code_img'];
                    $in_mobile=Db::table('os_user')->where(['id'=>$user['in_account_id']])->field('mobile,in_account_img')->find();
                    $user['in_account_img']=$in_mobile['account_code_img'];
                    $user['in_account_mobile']=$in_mobile['mobile'];
                    $return['data'] = $user;
                    return json($return);
                }else {
                    return json(array('Status' => 0, 'Message' => '账号或密码错误'));
                }
            } else {
                return json(array('Status' => 0, 'Message' => '账号或密码错误'));
            }
        }else{
            return json(['Status'=>0,'Message'=>'参数错误']);
        }
    }


    // 发送验证码
    public function Sendcode($username){
        $statusStr = array(
            "0" => "发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );
        $smsapi = "http://api.smsbao.com/";
        $user = "acme"; //短信平台帐号
        $pass = md5("acme2018"); //短信平台密码
        $code = rand(100000,999999);
        $content="[德信商城]尊敬的用户，您的验证码是".$code."，请于5分钟内正确输入。如非本人操作，请忽略此短信。";//要发送的短信内容
        $phone = $username;//要发送短信的手机号码
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $result =file_get_contents($sendurl) ;
        if($result == '0'){
//            $this->code_model = new CodeModel();
//            $where['username'] = $username;
//            $where['code'] = $code;
//            $where['time'] = time();
//            $this->code_model->create($where);
            return json(array('Status' => 1,'Message' => $statusStr[$result]));
        }else{
            return json(array('Status' => 0,'Message' => $statusStr[$result]));
        }

    }



    public function gettest(){
//        $smsapi = "http://api.smsbao.com/";
//        $user = "1498590195"; //短信平台帐号
//        $pass = md5("SST274805"); //短信平台密码
//        $code = rand(100000,999999);
//        $content="[超级人脉]尊敬的用户，您的验证码是".$code."，请于5分钟内正确输入。如非本人操作，请忽略此短信。";//要发送的短信内容
//        $phone = 17865635606;//要发送短信的手机号码
//        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
//        $result =file_get_contents($sendurl) ;
        return json(['asd'=>Cache::get('user'.'123'),'dsa'=>$_SERVER['SERVER_NAME'],'result'=>$result ]);
    }



    /*
     * 获取短信
     * mobile
     * */
    public function getmsg(){
        if ($this->request->isGet()) {
            $data=$this->request->get();
            $mobile=$data['mobile'];
            $statusStr = array(
                "0" => "发送成功",
                "-1" => "参数不全",
                "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
                "30" => "密码错误",
                "40" => "账号不存在",
                "41" => "余额不足",
                "42" => "帐户已过期",
                "43" => "IP地址限制",
                "50" => "内容含有敏感词"
            );
            $smsapi = "http://api.smsbao.com/";
            $user = "1498590195"; //短信平台帐号
            $pass = md5("SST274805"); //短信平台密码
            $code = rand(100000,999999);
            $content="[德信商城]尊敬的用户，您的验证码是".$code."，请于5分钟内正确输入。如非本人操作，请忽略此短信。";//要发送的短信内容
            $phone = $mobile;//要发送短信的手机号码
            $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
            $result =file_get_contents($sendurl) ;
            if($result == '0'){
                Cache::set('mobile'.$mobile,$code,300);
                return json(array('Status' => 1,'Message' => $statusStr[$result]));
            }else{
                return json(array('Status' => 0,'Message' => $statusStr[$result]));
            }

        }else{
            return json(['Status'=>0,'Message'=>'参数错误']);
        }
    }


    /*
     * 获取用户信息
     * id 用户id
     * */
    public function GetUserInfo(){
        if ($this->request->isGet()) {
            $userlogin=$this->request->get();
            $where['id'] = $userlogin['id'];
            $return[] = null;
            $user = Db::table('os_user')->where($where)->find();
            if ($user) {
                unset($user['password']);
                $user['account_code_img'] = 'http://' . $_SERVER['SERVER_NAME'] .'/'. $user['account_code_img'];
                $in_mobile=Db::table('os_user')->where(['id'=>$user['in_account_id']])->field('mobile,account_code_img')->find();
                $user['in_account_mobile']=$in_mobile['mobile'];
                $user['in_account_img']=$in_mobile['account_code_img'];
                $return['data'] = $user;
                return json(['Status'=>1,'Message'=>'获取成功','data'=>$return['data']]);
            } else {
                return json(array('Status' => 0, 'Message' => '获取信息失败'));
            }
        }else{
            return json(['Status'=>0,'Message'=>'参数错误']);
        }
    }
    /**
     * 保存用户
     * mobile
     * id 用户id
     * settype new新注册用户 old老用户修改
     */
    public function SetUserInfo()
    {
        if ($this->request->isGet()) {
            $data = $this->request->get();

            if($data['settype']=='new') {
                //验证手机号是否存在
                $check=Db::table('os_user')->where(['mobile'=>$data['mobile']])->find();
                if($check){
                    return json(['Status' => 0, 'Message' => '该手机号已存在']);
                }
                //短信验证码验证 mobile_msg
                $check_msg=Cache::get('mobile'.$data['mobile']);
                if($check_msg&&$check_msg==$data['mobile_msg']){
                    unset($data['mobile_msg']);
                }else{
                    return json(['Status'=>0,'Message'=>'验证码错误或验证码已失效！请重新尝试']);
                }
                //验证邀请人手机号
                if($data['in_account_mobile']){
                    $in_account=Db::table('os_user')->where(['mobile'=>$data['in_account_mobile']])->field('username,id')->find();
                    if(!$in_account){
                        return json(['Status'=>0,'Message'=>'邀请人手机号不存在']);
                    }
                    unset($data['in_account_mobile']);
                    $data['in_account_id']=$in_account['id'];
                    $data['in_account_name']=$in_account['username'];
                }



                //检查、组装数据
                unset($data['settype']);
                $data['create_time'] = date('Y-m-d H:i:s', time());
                if ($data['password'])
                    $data['password'] = md5($data['password']);
                else
                    return json(['Status' => 0, 'Message' => '密码不能为空']);
                if (!$data['username'])
                    return json(['Status' => 0, 'Message' => '姓名不能为空']);
                if (!$data['mobile'])
                    return json(['Status' => 0, 'Message' => '请输入手机号']);
                if (!$data['wechat_code'])
                    return json(['Status' => 0, 'Message' => '微信号不能为空']);
                $data['vip_level']=0;
                //保存
                $new_user_id = Db::table('os_user')->insertGetId($data);


                if ($new_user_id) {
                    $data['id']=$new_user_id;
                    return json(['Status' => 1, 'Message' => '新建用户成功','data'=>$data]);
                }else{
                    return json(['Status' => 0, 'Message' => '新建用户失败']);
                }
            }elseif ($data['settype']=='old'){
                unset($data['settype']);
                if (!$data['username'])
                    return json(['Status' => 0, 'Message' => '姓名不能为空']);
                if (!$data['wechat_code'])
                    return json(['Status' => 0, 'Message' => '微信号不能为空']);

                $r=Db::table('os_user')->where(['id'=>$data['id']])->update($data);

                if($r){
                    return json(['Status'=>1,'Message'=>'保存成功']);
                }else{
                    return json(['Status'=>0,'Message'=>'保存错误']);
                }
            }
        }else{
            return json(['Status' => 0, 'Message' => '参数错误']);
        }
    }
    /**
     *修改密码
     * id
     * password_old
     * password_new
     * password_check
     */
    public function changePassword()
    {
        if($this->request->isGet()){
            $data = $this->request->get();
            $old_password = Db::table('os_user')->where('id',$data['id'])->value('password');
            if ($old_password == md5($data['password_old'])) {
                if ($data['password_new'] == $data['password_check']) {
                    $new_password = md5($data['password_new']);
                    $res = Db::table('os_user')->where(['id' => $data['id']])->setField('password', $new_password);
                    if ($res !== false) {
                        return json(['Status'=>1,'Message'=>'修改成功']);
                    } else {
                        return json(['Status'=>0,'Message'=>'修改失败']);
                    }
                } else {
                    return json(['Status'=>0,'Message'=>'两次密码输入不一致']);
                }
            } else {
                return json(['Status'=>0,'Message'=>'原密码错误']);
            }
        }else{
            return json(['Status'=>0,'Message'=>'传参错误']);
        }
    }
    /**
     * 用户上传头像
     */
    public function updateImage()
    {
        if ($this->request->isGet()) {
            $data=$this->request->get();
            $where['id']=$data['id'];
            if (empty($data['image'])) {
                return json(['Status' => 0, 'Message' => '请选择上传文件']);
            } elseif (empty($where['id'])) {
                return json(['Status' => 0, 'Message' => '用户ID为空']);
            }
            $image=$data['image'];
            $imageName = "rm_".date("His",time())."_".rand(1111,9999).'i'.$where['id'].'.png';
            if (strstr($image,",")){
                $image = explode(',',$image);
                $image = $image[1];
            }
            $path = 'public/uploads/'.date("Ymd",time());
            if (!is_dir($path)){ //判断目录是否存在 不存在就创建
                mkdir($path,0777,true);
            }
            $imageSrc= $path."/". $imageName; //图片名字
            $r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
            if (!$r) {
                return json(['Status' => 0, 'Message' => '图片生成失败']);
            }else{
                Db::table('os_user')->where(['id'=>$data['id']])->update(['account_code_img'=>$imageSrc]);
                return json(['Status' => 1, 'Message' => '图片生成成功', 'Data' => $imageSrc]);
            }
        }
    }

    //客服信息
    public function kefu(){
        $info = Db::name('system')->field('value')->where('name', 'kefu')->find();
        $data = unserialize($info['value']);
        $data['image']='http://' . $_SERVER['SERVER_NAME'] .'/public/'. $data['image'];
        return json(['Status'=>1,'Message'=>'获取成功','data'=>$data]);
    }

}